import React from 'react'
// ===================================================================== antd
import Modal from '@antd/modal'
// ===================================================================== global declare
const { $http, $fn, $async } = window
// ===================================================================== global antd
const message = import('@antd/message')
const confirm = import('@antd/confirm')
// ===================================================================== global template

// ===================================================================== private template
const Page = $async(()=>import('#tp/page-content'))
// ===================================================================== private component
const SubmitForm = $async(()=>import('#cpt/submit-form'))
const Table = $async(()=>import('#cpt/table'))
// ===================================================================== config declare

// ===================================================================== component
export default class extends React.Component{
	state = {
		pag: {},
		data:[],
		selectedKeys:[],
		ids:[],
		btns : [
			{ label:'修改', disabled:true,  onClick: row => {
				this.refs.modal.open();
				const {selectedKeys } = this.state
				const data = selectedKeys[selectedKeys.length - 1]
				// this.setState({ editor:true, submit: $fn.setFormFields(submit, data) })
				this.setState({ editor:true })
				this.form.setFieldsValue(data)
				this.id = data.id
			} },
			{ label:'添加', onClick:() => {this.refs.modal.open(); this.setState({ editor:false })} },
			{ label:'删除', disabled:true, onClick:() => {
				confirm.then(f=>{
					f.default({
						msg:'确认删除？',
						onOk: close => {
							const ids = this.state.selectedKeys.map(v=>v['id'])
							$http.submit(null,'case/delete',{ param:{ id: ids }, loadingText:'删除中...' }).then(data=>{
								close()
								message.then(f=>f.default.success('删除中成功'))
								this.fetch(this.model)
								this.disBtns()
							})
						}
					})
				})
			} },
			{ label:'同步', onClick:() => {
				const ids = this.state.selectedKeys.map(v=>v['id'])
				$http.submit(this,'case/pull', { param:{ spec_code: ids }, loadingText:'数据同步中...' }).then(data=>{
					message.then(f=>f.default.success('数据同步成功'))
					this.fetch(this.model)
					this.disBtns()
				})
			} },
		],
		submit: [
			{ label: '条形码',		name: 'spec_code',			required: true, full:true},
			{ label: '送检医院',		name: 'hosp_id',			required: true, full:true, type:'select', data:[]},
			{ label: '姓名',		name: 'patient_name',		half:true},
			{ label: '性别',		name: 'sex',				half:true, type:'select', data:[]},
			{ label: '年龄一',		name: 'age_first_val',		half:true},
			{ label: '年龄类型一',	name: 'age_first_type',		half:true, type:'select', data:[]},
			{ label: '年龄二',		name: 'age_second_val',	half:true},
			{ label: '年龄类型二',	name: 'age_second_type',		half:true, type:'select', data:[]},
			{ label: '送检室',		name: 'department_name',	half:true},
			{ label: '床号',		name: 'bed_no',				half:true},
			{ label: '门诊/住院号',	name: 'outpatient',			half:true},
			{ label: '送检医生',		name: 'doctor',				half:true},
			{ label: '医生电话',		name: 'doctor_phone',		half:true},
			{ label: '受检人电话',	name: 'patient_phone',		half:true},
			{ label: '受检人类型',	name: 'case_type',			half:true, type:'select', data:[]},
			{ label: '标本性状',		name: 'spec_traits',		full:true, type:'select', data:[]},
			{ label: '医院标识',		name: 'hosp_identification',full:true},
			{ label: '身份证号',		name: 'card_no',			full:true},
			{ label: '采样日期',		name: 'coll_time',			full:true, type:'date-time', after:true},
			{ label: '临床诊断',		name: 'diagnosis_info',		full:true, type:'textarea'},
			{ label: '备注',		name: 'remark',				full:true, type:'textarea'},
			
		],
	}
	// 禁用按钮
	disBtns = () => {
		const { btns } = this.state
		btns[0].disabled = true;
		btns[2].disabled = true;
		this.setState({btns})
	}
	
	componentDidMount(){
		this.fetch()
		this.select()
	}
	model = {}
	// paging
	fetch = param => $fn.fetch.call(this,'case/index', param)
	// select
	select = async () => {
		const { submit } = this.state
		// 医院
		const hospId = await $fn.getSelect('dict/hospital')
		$fn.setSubmitSelect('hosp_id', submit, hospId)
		// 性别
		const sex = await $fn.getSelect('dict/sex')
		$fn.setSubmitSelect('sex', submit, sex)
		// 年龄类型
		const ageType = await $fn.getSelect('dict/age')
		$fn.setSubmitSelect('age_first_type', submit, ageType)
		$fn.setSubmitSelect('age_second_type', submit, ageType)
		// 受检人类型
		const caseType = await $fn.getSelect('dict/specType')
		$fn.setSubmitSelect('case_type', submit, caseType)
		// 标本性状
		const specTraits = await $fn.getSelect('dict/specTraits')
		$fn.setSubmitSelect('spec_traits', submit, specTraits)
		this.setState({ submit })
	}
	
	cols = [
		{ type:'checkbox' },
		{ title: '申请单条码', 	field: 'spec_code', 		width:160, align:'tc' },
		{ title: '受检人姓名', 	field: 'patient_name', 		width:150 },
		{ title: '性别', 		field: 'sex', 				width:150, align:'tc' },
		{ title: '年龄', 		field: 'age_first_val', 	width:150, align:'tc', render:({ text, rows })=> text + rows['age_first_type_name'] },
		{ title: '受检人电话', 	field: 'patient_phone', 	width:150 },
		{ title: '身份证号', 	field: 'card_no', 			width:150, align:'tc' },
		{ title: '送检单位', 	field: 'hosp_name',			width:250},
		{ title: '送检科室', 	field: 'department_name',	width:150},
		{ title: '送检医生', 	field: 'doctor',			width:150},
		{ title: '医生电话', 	field: 'doctor_phone',	 	width:150, align:'tc'},
		{ title: '采样时间', 	field: 'coll_time',	 		width:150, align:'tc'},
		{ title: '审核状态', 	field: 'status',			align:'tc', render: ({ text }) =>{
			if(+text === 1){ return '待审核' }
			else if(+text === 2){ return '审核通过' }
			else{ return '待完善' }
		}},
	]
	
	render(){
		const { btns, data, pagingLoading, pag, submit, editor } = this.state
		return (
			<Page title='用户信息' btns={btns}>
				<Table 
					cols			= { this.cols }
					data 			= { data }
					loading 		= { pagingLoading }
					onRow			= { selectedKeys => {
						this.setState({ selectedKeys },()=>{
							if($fn.hasArray(this.state.selectedKeys)){
								btns[0].disabled = false;
								btns[2].disabled = false;
								this.setState({btns})
							}else{
								this.disBtns()
							}
						})
					} }
					drag
					pag				= { pag }
					onChange		= { (current, pageSize) => $fn.pageChange.call(this,{current, pageSize}) }
				/>
				
				<Modal 
					ref		= 'modal' 
					title	= {editor ? '修改' : '添加'} 
					width	= {700} 
					noFooter
					onClose = { () => this.form && this.form.resetFields() } 
				>
					<SubmitForm
						modal
						data = { submit }
						onSubmit = { v => {
							if (editor) {
								const param = { ...v, id: this.id}
								$http.submit(null,'case/edit',{ param }).then(data=>{
									message.then(f=>f.default.success('修改成功'))
									this.refs.modal.close()
									this.fetch(this.model)
								})
							} else {
								const param = { ...v }
								$http.submit(null, 'case/add', { param }).then(data => {
									message.then(f => f.default.success('添加成功'))
									this.refs.modal.close()
									this.fetch(this.model)
								})
							}
						}}
						onClose = { () => this.refs.modal.close()  }
						init = { form => this.form = form }
					 />
				</Modal>
			</Page>
		)
	}
}